package com.note.simpleadmin;
import com.note.simpleadmin.cache.EnCacheUtil;
import com.note.simpleadmin.entity.TbEnvironment;
import com.note.simpleadmin.module.system.service.EnvironmentService;
import com.note.simpleadmin.utils.DateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import javax.annotation.PostConstruct;
import java.util.List;

@Configuration
public class CacheConfig {
    private static Logger logger = LogManager.getLogger(FreeMarkerConfig.class);
    @Autowired
    private freemarker.template.Configuration configuration;
    @Autowired
    private EnvironmentService environmentService;
    @PostConstruct
    public void setConfigure() throws Exception {
        logger.info("####################初始化字典##########startTime={}###########", DateUtil.getALLTime());
        long start = System.currentTimeMillis();
        List<TbEnvironment> environmentList=environmentService.findAll();
        for(TbEnvironment environment:environmentList)
        {
            EnCacheUtil.put(environment.getType()+"_"+environment.getName(),environment.getValue());
            configuration.setSharedVariable(environment.getType()+"_"+environment.getName(),environment.getValue());
        }
        long end = System.currentTimeMillis();
        logger.info("###################字典初始化完成########endTime={}#############",DateUtil.getALLTime());
        logger.info("###################字典初始化耗时########time={}ms#############",end-start);
    }
}